from fastapi import APIRouter, Depends, Body, Path

import typing as T
from ...pojo.vo.Result import Result
from ...pojo.dto.UserSessionDTO import UserSessionDTO
from ..service.impl.UserSessionServiceImpl import UserSessionService


router = APIRouter(prefix="/session", tags=["用户会话接口"])


@router.post("", response_model=Result)
async def createSession(
    service: UserSessionService,
    userSessionDTO: T.Annotated[UserSessionDTO, Body(description="用户会话窗口的参数")],
):
    """
    # 创建会话窗口

    请求体中必须传递用户的id，会话窗口的名称【可选】

    ## 请求体示例

    ```json
    {
        "userId": 1,
        "alias": "我的会话窗口"
    }
    ```

    ## 返回示例

    ```json
    {
        "code": 1,
        "data": null,
        "msg": "success"
    }
    ```
    """
    await service.createSession(userSessionDTO)
    return Result.success()


@router.get("/{id}", response_model=Result)
async def getSessionByUserId(
    service: UserSessionService, id: T.Annotated[int, Path(description="用户id")]
):
    """
    # 获取会话窗口列表

    根据用户的id获取用户的会话窗口列表

    ## 返回示例

    ```json
    {
        "code": 1,
        "data": [
            {
                "id": 1,
                "userId": 1,
                "alias": "我的会话窗口",
                "createTime": "2023-07-09T09:09:09.000+00:00",
            }
        ],
        "msg": "success"
    }
    """
    result = await service.getSessionByUserId(id)
    return Result.success(result)


@router.put("", response_model=Result)
async def updateSessionNameBySessionId(
    service: UserSessionService,
    sessionId: int = Body(description="会话id"),
    name: str = Body(description="要更改的会话名称"),
):
    """
    # 修改会话窗口名称

    根据会话id修改会话窗口名称

    ## 请求体示例

    ```json
    {
        "sessionId": 1,
        "name": "我的会话窗口"
    }
    ```
    ## 返回示例

    ```json
    {
        "code": 1,
        "data": null,
        "msg": "success"
    }
    """
    await service.updateSessionNameBySessionId(sessionId, name)
    return Result.success()


@router.delete("/{sessionId}", response_model=Result)
async def deleteBySessionId(
    service: UserSessionService, sessionId: T.Annotated[int, Path(description="会话id")]
):
    """
    # 删除会话窗口

    根据会话id删除会话窗口

    ## 返回示例

    ```json
    {
        "code": 1,
        "data": null,
        "msg": "success"
    }
    ```
    """
    await service.deleteSessionBySessionId(sessionId)
    return Result.success()
